class SearchController < ApplicationController
    
    layout 'default'
    
    def initialize
        @results_limit = 50
    end
    
    #########################################################
    # Show search page if there is nothing specified to search for
    # or if there are searchable params search for them
    def index
        @page_title = "Advanced Search"
        render
    end
    
    #########################################################
    # search for the given terms
    def search_results
        search_type = params[:search][:type] || 'all'
        search_terms = params[:search][:terms]
        do_search(search_terms, search_type)
    end
    
    protected
    
    #########################################################
    # search all fields for the given terms
    def do_search(search_terms, search_type = 'all')
        
        if search_terms
            @terms = search_terms.collect {|term| term}
            sql_terms = search_terms.collect {|term| "%" + term + "%"}.join(' OR ').downcase

            search_conditions = 'SELECT DISTINCT ON (annorations.id) annorations.*, users.login, pages.average_rating, pages.total_ratings '
            search_conditions << 'FROM annorations ' 
            search_conditions << 'INNER JOIN pages ON pages.id = annorations.page_id '
            search_conditions << 'INNER JOIN users ON users.id = annorations.user_id '
                
            if search_type == 'all'
                search_conditions << "WHERE ((lower(annorations.title) LIKE ? ) "
                search_conditions << 'OR (lower(annorations.comment) LIKE ? ) '
                search_conditions << 'OR (lower(pages.uri) LIKE ? ) '
                search_conditions << 'OR (lower(users.login) LIKE ? )) ' 
                sql_term_array = [sql_terms, sql_terms, sql_terms, sql_terms]
                
            elsif search_type == 'titles'
                search_conditions << 'WHERE (lower(annorations.title) LIKE ? )' 
                sql_term_array = [sql_terms]
                
            else search_type == 'contents'
                search_conditions << 'WHERE ((lower(annorations.title) LIKE ? ) OR (lower(annorations.comment) LIKE ? ) OR (lower(users.login) LIKE ? ))' 
                sql_term_array = [sql_terms, sql_terms, sql_terms]
                
            end

            search_conditions << 'ORDER BY annorations.id DESC '
            search_conditions << 'LIMIT ' + @results_limit.to_s

            @results = Annoration.find_by_sql([search_conditions] + sql_term_array)
            if @results.length <= 0
                @page_title = "No Annorations Found for: " + @terms.to_sentence
            else
                @page_title = "Annorations found for: " + @terms.to_sentence
            end
            
        else
            if params[:terms].nil?
                @page_title = 'No Annorations found for: ' + params[:terms]
            else
                @page_title = 'If you want me to find something you have to tell me what to look for.'
            end
            @results = []
        end
                
        self.render_results
    end

    #########################################################
    # all the results are rendered the same
    def render_search
        respond_to do |format|
            format.html { render :template => '/search/index' }
            format.xml  { render :xml => flash.to_xml }
            format.rss  { render :xml => flash.to_xml }
            format.atom  { render :xml => flash.to_xml }
            format.rdf  { render :xml => flash.to_xml }
        end
    end
        
    #########################################################
    # all the results are rendered the same
    def render_results
        respond_to do |format|
            format.html { render :template => '/search/results' }
            format.xml  {@results = @user.annorations; render :action => '../shared/annorations_xml.rxml', :layout=>false }
            format.rss  {@results = @user.annorations; render :action => '../shared/annorations_rss.rxml', :layout=>false }
            format.atom {@results = @user.annorations; render :action => '../shared/annorations_atom.rxml', :layout=>false }
            format.rdf {@results = @user.annorations; render :action => '../shared/annorations_rdf.rxml', :layout=>false }
        end
        
    end
    
end
